﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
namespace HZ.Supervise.Web.Secondphase.ReportArea
{
    public partial class GoodsList : BasePage
    {
        string sortField = "ProcureCatalogID ASC";
        string append = "";
        bool hide = true;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
                {
                    if (sq.SortSource == 0)         //上个页面传过来的session，然后赋给本页面的dropSort。
                    {
                        droSort.SelectedIndex = 1;
                    }
                    else if (sq.SortSource == 1)
                    {
                        droSort.SelectedIndex = 2;
                    }
                    else if (sq.SortSource == 2)
                    {
                        droSort.SelectedIndex = 3;
                    }
                    BindList();
                }
                else
                {
                    Response.Write("<script>history.go(-1);<script>");
                }
            }
        }

        /// <summary>
        /// 查询数据与绑定数据
        /// </summary>
        void BindList()
        {
            int t = 0;
            string where = "";
            string order = "";
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            SuperviseQuery sq = new SuperviseQuery();
            string strAreaId = query.AreaID;
            if (strAreaId.Substring(4,2).Equals("00"))
            {
                strAreaId = strAreaId.Substring(0, 4);
            }
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            //页面条件
            if (this.txtProductName.Text.Trim() != "")
            {
                where += "AND ProductName like '%" + this.txtProductName.Text.Trim() + "%'";
            }
            if (this.txtMedicineModelName.Text.Trim() != "")
            {
                where += "and MedicineModel like '%" + this.txtMedicineModelName.Text.Trim()  + "%'";
            }
            if (this.txtPack.Text.Trim() != "")
            {
                where += "and Pack like '%" + this.txtPack.Text.Trim() + "%'";
            }
            if (this.txtCompanyName_SC.Text.Trim() != "")
            {
                where += "and CompanyName_SC like '%" + this.txtCompanyName_SC.Text.Trim() + "%'";
            }
            if (this.txtUnit.Text.Trim() != "")
            {
                where += "and Unit like '%" + this.txtUnit.Text.Trim() + "%'";
            }
            if (!this.droSort.SelectedValue.Equals("-1"))
            { 
                where +="and SortSource='"+droSort.SelectedValue+"'";
            }

            where += "and AreaId like'" + strAreaId + "%'";
            where += " and DayTime like'%" + query.Mid + "%'";

            order += droSortField.SelectedValue;

            //将数据填入gridview中
            System.Data.DataTable dt = ie.ExecuteDetailsArea(where,order, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            this.AspNetPager1.RecordCount = t;
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            gvwGoods.DataSource = dt;
            gvwGoods.DataBind();
            HZ.Supervise.Web.Function.Combine(gvwGoods, 0, 0);
        }

        //绑定
        protected void gvwGoods_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        /// <summary>
        /// 点击详情跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwGoods_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                try
                {
                    query.ProcureCatalogID = e.CommandArgument.ToString();
                    query.SortSource = Convert.ToInt32(this.droSort.SelectedValue);
                    query.ProductName = null;
                    query.HospitalName = null;
                    query.MedicineModelName = null;
                    //query.SortSource = null;
                    query.Outlookc = null;
                    query.Unit = null;
                }
                catch { }
                Session[sessionid] = query;
                string url = "AreaGoodsDetail.aspx";
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }
        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 合并表头
        /// by张雄  2011.5.7
        /// </summary>
        /// <param name="sender">事件的发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwGoods_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    //第一行表头
                    TableCellCollection tbHeader = e.Row.Cells;
                    tbHeader.Clear();
                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[0].Attributes.Add("colspan", "12");
                    tbHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

                    if (!string.IsNullOrEmpty(sq.AreaName))
                    {

                        tbHeader[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + " " + sq.AreaName + "</th></tr><tr>";

                    }
                    else
                    {

                        tbHeader[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + "</th></tr><tr>";

                    }

                    //第2行表头
                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[1].Text = "采购类型";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[2].Text = "通用名";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[3].Text = "剂型";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[4].Text = "采购规格";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[5].Text = "采购单位";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[6].Text = "生产企业";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[7].Text = "采购金额";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[8].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[8].Text = "配送金额 ";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[9].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[9].Text = "配送率";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[10].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[10].Text = "入库金额";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[11].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[11].Text = "入库率";

                    tbHeader.Add(new TableHeaderCell());
                    tbHeader[12].Attributes.Add("bgcolor", "Khaki");
                    tbHeader[12].Text = "操作";
                    break;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

		protected void btnExcel_Click(object sender, EventArgs e)
		{
			int t = 0;
			string where = "";
			string order = "";
			SuperviseQuery query = (SuperviseQuery)ViewState["query"];
			SuperviseQuery sq = new SuperviseQuery();
			string strAreaId = query.AreaID;
			if (strAreaId.Substring(4, 2).Equals("00"))
			{
				strAreaId = strAreaId.Substring(0, 4);
			}
			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            if (this.txtProductName.Text.Trim() != "")
            {
                where += "AND ProductName like '%" + this.txtProductName.Text.Trim() + "%'";
            }
            if (this.txtMedicineModelName.Text.Trim() != "")
            {
                where += "and MedicineModel like '%" + this.txtMedicineModelName.Text.Trim() + "%'";
            }
            if (this.txtPack.Text.Trim() != "")
            {
                where += "and Pack like '%" + this.txtPack.Text.Trim() + "%'";
            }
            if (this.txtCompanyName_SC.Text.Trim() != "")
            {
                where += "and CompanyName_SC like '%" + this.txtCompanyName_SC.Text.Trim() + "%'";
            }
            if (this.txtUnit.Text.Trim() != "")
            {
                where += "and Unit like '%" + this.txtUnit.Text.Trim() + "%'";
            }
            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                where += "and SortSource='" + droSort.SelectedValue + "'";
            }
            where += "and AreaId like'" + strAreaId + "%'";
            where += " and DayTime like'%" + query.Mid + "%'";

			order += droSortField.SelectedValue;
			//将数据填入gridview中
			System.Data.DataTable dt = ie.ExecuteDetailsArea(where, order, 1, 0, out t);
			this.AspNetPager1.RecordCount = t;
			HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
			HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

			HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwGoods);
		}
    }
}
